// JavaScript Document
function searchForm(obj)
{
	var keyword = $(obj).find('input[name=q]'); 
	if( keyword.val() =='')
	{
		return false;
	}
	return true;
}
$(function(){
	$('#search_bar input[name=q]').bind('click',function(){
		if($(this).val()!=''){
			$(this).css({"color":"#000"});	
		}
	});	
	$('#search_bar input[name=q]').bind('change',function(){
		if($(this).val()!=''){
			$(this).css({"color":"#000"});	
		}
	});
	$('#search_bar input[name=q]').bind('blur',function(){
		if($(this).val()!=''){
			$(this).css({"color":"#000"});	
		}
	});
});

(function() {
  var mask;
  var popup;
  var current = '';
  var popup_mark = 'g_reg'

  var init_popup = function() {
    mask = $('<div class="popup-reg-mask"></div>').appendTo('body');
    mask.css('height', $(document).height());
    popup = $('#g-popup-reg');
    popup.find('.lnk-close').click(function(e) {
        e.preventDefault();
        popup.slideUp(400);;
        mask.hide();
        if (window.POPUP_REG) {
          window.name = window.name || popup_mark;
        }
    });
    if ($.browser.msie && ($.browser.version|0) === 6) {
        var win = $(window).scroll((function() {
            var timer;
            var doc = document.body;
            return function() {
                if (timer) {
                    window.clearTimeout(timer);
                }
                timer = window.setTimeout(function() {
                    popup.css({
                      top: (doc.scrollTop + win.height()/2 - popup.height()/2) + 'px'
                    });
                }, 20);
            };
        })()).trigger('scroll');
    }
  };

  var show_popup = function() {
    if (popup) {
      popup.show();;
      mask.show();
    } else {
      $('#g-popup-reg').show();
      init_popup();
    }
  };
  //公用登录
  IKCOM.init_show_login = function (e) {
    var node = $(e);
    node.click(function(e) {
      e.preventDefault();
      show_popup();
      if (current !== 'login') { 
        popup.find('iframe').attr('src', show_login_url);
      }
      current = 'login';
    });
  };
  //公用注册
  IKCOM.init_show_register = function (e) {
    var node = $(e);
    node.click(function(e) {
      e.preventDefault();
      show_popup();
      if (current !== 'register') {
        popup.find('iframe').attr('src', show_register_url);
      }
      current = 'register';
    });
  };
    // 有些页面会自动弹注册框
  if (window.POPUP_REG) {
    $(function() {
      show_popup();
      popup.find('iframe').attr('src', show_register_url);
      current = 'register';
    });
  }
  
})();

/*显示隐藏回复*/
function commentOpen(id,gid)
{
	$('#rcomment_'+id).slideToggle('fast');
}
//安全性检测 回应帖子
function checkComment(obj)
{
	var isTrue = true;
	var c = obj.find('textarea[name=content]').val();
	
	if(c == ''){ alert('你回应的内容不能为空'); isTrue = false;}
	if(c.length > 2000){ alert('你已经输入了'+c.length+'个字；你回应的内容不能超过2000个字。');isTrue = false;}
	
	if(isTrue){
		obj.find('input[type=button]').val('正在提交^_^').attr('disabled',true);
	}
	return isTrue;
}
//Ctrl+Enter 回应
function keyComment(obj,event)
{
	 var form = $(obj).parents('form');
     if(event.ctrlKey == true)
	 {
		if(event.keyCode == 13)
		if(checkComment(form))
		{
			addComment(obj);
		}
	}
}
function keyRecomment(rid,tid,type,event)
{
     if(event.ctrlKey == true)
	 { 
		if(event.keyCode == 13)
		recomment(rid,tid,type);
		return false;
	}
}
//回复评论
function addComment(obj){
	var arrData = {}, self = $(obj), formMini = self.parents('form');
	
	var content = formMini.find('textarea[name=content]').val();
	var referid = formMini.attr('referid');
	var postUrl = formMini.attr('postAction');
	var base64Data = formMini.find('input[name=base64Data]').val();
	var tokenKey   = $('input[name=tokenKey]').val();
	var tokenValue = $('input[name=tokenValue]').val();
	
	if(checkComment(formMini)){
		//组合数据
		arrData.content = $.trim(content);
		arrData.referid = $.trim(referid);	
		arrData.base64Data = $.trim(base64Data);	
		arrData.tokenKey = $.trim(tokenKey);
		arrData.tokenValue = $.trim(tokenValue);
		
		$.post(postUrl, arrData, function(result){
			if(result.status == 1){
				formMini.find('input[type=button]').val('加上去(Crtl+Enter)').removeAttr('disabled');
				error(result.text);
			}else if(result.status == 0){
				succ(result.text);
				window.location.reload();
			}
		});	
	}
}
//删除评论
function delComment(obj, cid){
	var arrData = {}, self = $(obj), cli = $('#cli_'+cid);
	
	var cid = cid;
	var uname = self.attr('data-name');
	var postUrl = self.attr('data-url');
	var tokenKey   = $('input[name=tokenKey]').val();
	var tokenValue = $('input[name=tokenValue]').val();
		
	var isDel = false;
	isDel = confirm("确定要删除【"+uname+"】的留言吗？");
	if(cid > 0 && isDel){ 
		//组合数据
		arrData.cid = $.trim(cid);
		arrData.tokenKey = $.trim(tokenKey);
		arrData.tokenValue = $.trim(tokenValue);
				
		$.post(postUrl, arrData, function(result){
			if(result.status == 1){
				error(result.text);
			}else if(result.status == 0){
				cli.slideUp(200);
			}
		});	
	}
}
